package com.zyy.smartcity1129.me;

import android.annotation.SuppressLint;
import android.content.ContentResolver;
import android.content.ContentUris;
import android.content.Context;
import android.content.Intent;
import android.content.SharedPreferences;
import android.database.Cursor;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.net.Uri;
import android.provider.DocumentsContract;
import android.provider.MediaStore;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.Spinner;
import android.widget.Toast;

import androidx.annotation.Nullable;

import com.bumptech.glide.Glide;
import com.google.gson.JsonObject;
import com.zyy.smartcity1129.BaseFragment;
import com.zyy.smartcity1129.MainActivity;
import com.zyy.smartcity1129.R;
import com.zyy.smartcity1129.data.FileInfo;
import com.zyy.smartcity1129.data.ResultInfo;
import com.zyy.smartcity1129.data.UserInfo;
import com.zyy.smartcity1129.util.Http;
import com.zyy.smartcity1129.util.XCUtli;

import java.io.File;

import okhttp3.MediaType;
import okhttp3.MultipartBody;
import okhttp3.RequestBody;

/**
 * @ClassName: MyDataFragment$
 * @Description: 作用
 * @Author: letquit
 * @CreateDate: 1/12/2021$
 * @Version: 1.0
 */
public class MyDataFragment extends BaseFragment {
    private ImageView imgMyData;
    private EditText tvMyDataNick;
    private Spinner spMySex;
    private EditText tvMyPhone;
    private Button btnMyReplace;
//    private UserInfo.UserBean user = MainActivity.user;
    String imgUrl;
    File file;

    private UserInfo.UserBean user = MeFragment.userBean;

    @Override
    protected int layId() {
        return R.layout.f_mydata;
    }

    @Override
    protected String title() {
        return "个人信息";
    }

    @Override
    protected boolean back() {
        return true;
    }

    @Override
    protected Boolean bottom() {
        return null;
    }

    /*
     *上传头像前要动态请求权限，系统签名也要加权限
     */
    @SuppressLint("Range")
    @Override
    public void onActivityResult(int requestCode, int resultCode, @Nullable Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        Glide.with(imgMyData.getContext()).load(data.getExtras().get("data")).into(imgMyData);
        ContentResolver resolver = getActivity().getContentResolver();
        Cursor cursor = resolver.query(data.getData(), new String[]{MediaStore.MediaColumns.DATA}, null, null, null);
        cursor.moveToNext();
        imgUrl = cursor.getString(cursor.getColumnIndex(MediaStore.MediaColumns.DATA));
        Log.i("TAG", "onActivityResult: " + imgUrl);
        Glide.with(imgMyData.getContext()).load(imgUrl).into(imgMyData);
        file = new File(imgUrl);
    }

    @Override
    protected void init(View iv) {
        initView(iv);
        Glide.with(imgMyData.getContext()).load(user.getAvatar()).into(imgMyData);
        imgMyData.setOnClickListener(view -> {
            Intent intent = new Intent(Intent.ACTION_PICK, MediaStore.Images.Media.EXTERNAL_CONTENT_URI);
            startActivityForResult(intent, 1);
        });
        btnMyReplace.setOnClickListener(view -> {
            String sex = spMySex.getSelectedItem().toString();
            if (sex.equals("男")) {
                sex = "0";
            } else if (sex.equals("女")){
                sex = "1";
            }
            String nickName = tvMyDataNick.getText().toString().trim();
            String phoneNum = String.valueOf(tvMyPhone.getText().toString().trim());
            if (!sex.equals("") && !nickName.equals("") && !phoneNum.equals("")) {
                JsonObject jsonObject = new JsonObject();
                jsonObject.addProperty("nickName", nickName);
                jsonObject.addProperty("sex", sex);
                jsonObject.addProperty("phonenumber", phoneNum);
                Log.i("TAG1", "init: "+file.toString());
                th(() ->{
                    String result = Http.getInstance().get("/prod-api/api/common/user", RequestBody.create(Http.m, jsonObject.toString()), ResultInfo.class, 3).getMsg();
                    ui(() ->{
                        if (result.equals("操作成功")) {
                            Toast.makeText(getContext(), result, Toast.LENGTH_SHORT).show();
                            remove();
                        } else {
                            Toast.makeText(getContext(), "操作失败，检查输入", Toast.LENGTH_SHORT).show();
                        }
                    });
                });
            } else {
                Toast.makeText(getContext(), "请完善", Toast.LENGTH_SHORT).show();
            }

        });
    }

    private void initView(View iv) {
        imgMyData = (ImageView) iv.findViewById(R.id.imgMyData);
        tvMyDataNick = (EditText) iv.findViewById(R.id.tvMyDataNick);
        spMySex = (Spinner) iv.findViewById(R.id.spMySex);
        tvMyPhone = (EditText) iv.findViewById(R.id.tvMyPhone);
        btnMyReplace = (Button) iv.findViewById(R.id.btnMyReplace);
    }
}
